package com.fr.file;

import java.io.BufferedReader;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;

/**
 * @author 付荣
 * @version 创建时间：2019年7月2日 上午10:57:51
 */
public class StatisticsSuggest {
    public static void main(String[] args) throws Exception {
        FileReader fr = new FileReader("E:\\test\\catalina.out-2019-07-02");
        OutputStream out = new FileOutputStream("E:\\test\\StatisticsSuggest.xlsx");
        ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
        BufferedReader br = new BufferedReader(fr);
        String line = br.readLine();
        Map<String, Map<String, Integer>> map = new HashMap<>();
        Map<String, Integer> map1 = new HashMap<>();
        while (line != null) {
            int index = line.indexOf("suggest调用统计 - ");
            if (index >= 0) {
                int index1 = line.indexOf(":", index);
                if (index1 > 0) {
                    int index2 = line.indexOf("}", index1);
                    String mothed = line.substring(index + "suggest调用统计 - ".length(), index1);
                    String param = line.substring(index1 + 1, index2 + 1);

                    Map<String, Integer> tmp = map.get(mothed);
                    if (tmp == null) {
                        tmp = new HashMap<>();
                        map.put(mothed, tmp);
                    }
                    Integer count = tmp.get(param);
                    if (count == null) {
                        count = 0;
                    }
                    count++;
                    tmp.put(param, count);
                    Integer t = map1.get(mothed);
                    if (t == null) {
                        t = 0;
                    }
                    t++;
                    map1.put(mothed, t);
                }
            }
            line = br.readLine();
        }
        br.close();
        fr.close();
        int i = 0;
        for (Map.Entry<String, Map<String, Integer>> entry : map.entrySet()) {
            // System.out.println(entry.getKey() + ",总数：" + entry.getValue().size());
            i++;
            Sheet sheet = new Sheet(i);
            sheet.setSheetName(entry.getKey());
            List<Map.Entry<String, Integer>> list = new ArrayList<>();
            list.addAll(entry.getValue().entrySet());

            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    return o2.getValue() - o1.getValue();
                }

            });
            List<List<String>> l = new ArrayList<>();
            List<String> total = new ArrayList<>();
            total.add(entry.getKey());
            total.add(String.valueOf(map1.get(entry.getKey())));
            int to = map1.get(entry.getKey());
            l.add(total);
            for (Map.Entry<String, Integer> e : list) {
                SuggestQueryBean bean = JSON.parseObject(e.getKey(), SuggestQueryBean.class);
                // System.out.println(e.getKey() + ",次数：" + e.getValue());
                List<String> ll = new ArrayList<>();
                ll.add(bean.getKeyword());
                ll.add(e.getKey());
                ll.add(String.valueOf(e.getValue()));
                ll.add(String.valueOf(e.getValue().doubleValue() / to));
                l.add(ll);
            }
            writer.write0(l, sheet);
        }
        writer.finish();
        out.close();
    }

}
